package Email.auto.checker;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class Account extends JFrame {

	private Dimension screenDim;
	private Container cp;
	private JButton buttonOk;
	private JPanel panelSouth;
	private JPanel panelTest;
	private JTextField textEmail;
	private String domain;
	private JPasswordField textPass;
	private Tray traymenu;
	private Comunication comunication;
	
	private String email;
	private String pass;
	
	public Account(Tray tray, Comunication com) {
		traymenu = tray;
		comunication = com;
		cp = getContentPane();
		cp.setLayout(new BorderLayout());
		makeObject();
		arrangeComponents();
		addListener();

		this.setSize(new Dimension(ConstAccount.WIDTH_WINDOW,
				ConstAccount.HEIGHT_WINDOW));

		screenDim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(
				(int) (screenDim.getWidth() - ConstAccount.WIDTH_WINDOW) / 2,
				(int) (screenDim.getHeight() - ConstAccount.HEIGHT_WINDOW) / 2);

		this.setUndecorated(false);
		

	}
	
	public void setVisible(){
		this.setVisible(true);
	}
	
	public void setInvible(){
		this.setVisible(false);
	}
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	JTextField getTextEmail() {
		return textEmail;
	}

	JPasswordField getTextPass() {
		return textPass;
	}

	private void addListener() {
		buttonOk.addActionListener(new ClickButtonOk(this, comunication, traymenu));
		
	}
	
	public void setDomain(String dom){
		domain = dom;
	}
	public String getDomain(){
		return domain;
	}

	private void arrangeComponents() {
		panelSouth.add(buttonOk);
		panelTest.add(textEmail);
		panelTest.add(textPass);
		cp.add(panelSouth, BorderLayout.SOUTH);
		cp.add(panelTest, BorderLayout.CENTER);
	}

	private void makeObject() {
		panelTest = new JPanel(new GridLayout(ConstAccount.TAM_GRIDLAYOUT_NROW,
				ConstAccount.TAM_GRIDLAYOUT_NCOL));
		new JPanel(new BorderLayout());
		textEmail = new JTextField("",
				ConstAccount.TAM_TEXTBOX);
		textEmail.setBorder(BorderFactory.createTitledBorder("Email"));
		textPass = new JPasswordField(ConstAccount.TAM_TEXTBOX);
		textPass.setBorder(BorderFactory.createTitledBorder("Password"));
		new JPanel(new BorderLayout());
		new JLabel("Email:");
		new JLabel("Password:");

		panelSouth = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonOk = new JButton("OK");

	}
	
	public Tray getTray(){
		return traymenu;
	}

	public void setTextEmail(String email) {
		this.textEmail.setText(email);
	}

	public void setTextPass(String pass) {
		this.textPass.setText(pass);
	}
	
	public String getDomainFromEmail(String emailString){
		String[] part= {"part1","part2"};
		String domain = null;
		
		System.out.println("ValidaActualUser: " +emailString);
		if (emailString.contains("@")) {
			part = emailString.split("@");
			domain = part[1];
		}
		return domain;
	}

}
